<template>
  <main
    class="header-container"
    v-loading.fullscreen.lock="fullscreenLoading"
    :element-loading-text="loadingText"
  >
    <div>
      <a class="home-menu-link link-item" href="/">
        <el-icon size="24">
          <HomeFilled />
        </el-icon>
      </a>
    </div>
    <div style="flex-grow: 1; display: flex; flex-direction: row-reverse">
      <div class="login" v-if="oidc.isLogin">
        <!-- 用户头像 -->
        <el-avatar class="user-avator" :size="30" :src="imgurl" />
        <!-- 用户名下拉菜单 -->
        <el-dropdown trigger="click" @command="onCommand">
          <div class="el-dropdown-link">
            <el-text class="text-margin">admin</el-text>
            <el-icon>
              <arrow-down />
            </el-icon>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <a href="https://code.catlbattery.com/root/flying-eye-system" target="_blank">
                <el-dropdown-item>项目仓库</el-dropdown-item>
              </a>
              <a href="https://code.catlbattery.com/root/flying-eye-system" target="_blank">
                <el-dropdown-item>官方文档</el-dropdown-item>
              </a>
              <!-- <el-dropdown-item command="user">个人中心</el-dropdown-item> -->
              <el-dropdown-item divided command="loginout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
      <div v-else class="login" @click="onLogin">
        <el-tooltip effect="dark" content="用户登录" placement="bottom">
          <el-icon class="login-icon" :size="24" color="#231815"><UserFilled /></el-icon>
        </el-tooltip>
      </div>
      <nav class="navbar-menu">
        <router-link
          v-for="routerLinkItem in linkItems"
          :key="routerLinkItem.label"
          class="router-link-item"
          :to="routerLinkItem.path"
          @click="onChecked(routerLinkItem)"
          :class="{ active: routerLinkItem.isChecked }"
        >
          {{ routerLinkItem.label }}
        </router-link>
      </nav>
    </div>
  </main>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import { useOidcStore } from '@/stores/oidc';
// import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import imgurl from '@/assets/img.jpg';

const fullscreenLoading = ref(false);
const loadingText = ref('页面加载中...');

interface LinkItem {
  label: string;
  path: string;
  isChecked: boolean;
}

const linkItems = ref<LinkItem[]>([
{
    label: '用户管理',
    path: '/users',
    isChecked: false
  },
  {
    label: '项目管理',
    path: '/project',
    isChecked: false
  },
  {
    label: '分布式设备',
    path: '/distributed-devices',
    isChecked: false
  },
  {
    label: '动态保养',
    path: '/',
    isChecked: false
  },
  {
    label: '设备易损件',
    path: '/',
    isChecked: false
  }
]);

const oidc = useOidcStore();

const onChecked = (linkItem: LinkItem) => {
  const checkedItem = linkItems.value.find((item) => item.isChecked);
  if (checkedItem != null && checkedItem != linkItem) {
    checkedItem.isChecked = false;
  }
  linkItem.isChecked = true;
};

const onLogin = async () => {
  // const router = useRouter();
  // oidc.userManager
  //   .signinRedirect()
  //   .then(() => {
  //     ElMessage.info('login...');
  //     // router.push('/');
  //     // if (checked.value) {
  //     //   localStorage.setItem('login-param', JSON.stringify(param))
  //     // } else {
  //     //   localStorage.removeItem('login-param')
  //     // }
  //   })
  //   .catch((err) => {
  //     console.log(err);
  //     // ElMessage.error('登录失败')
  //     // return false
  //   });

  fullscreenLoading.value = true;
  loadingText.value = '正在登录...';
  try {
    await oidc.userManager.signinRedirect();
  } catch (err) {
    ElMessage.error('登录错误');
  } finally {
    fullscreenLoading.value = false;
    loadingText.value = '页面加载中...';
  }
};

const onCommand = (command: string) => {
  if (command == 'loginout') {
    oidc.signout();
  }
};
</script>

<style lang="scss" scoped>
.header-container {
  height: var(--header-height);
  width: 100%;
  display: flex;
  align-items: center;
  padding: 0 24px 0 24px;
  position: relative;
}

@media screen and (min-width: 768px) {
  .header-container {
    padding: 0 24px 0 32px;
  }
}

@media screen and (min-width: 1680px) {
  .header-container {
    padding: 0 48px;
  }
}

.navbar-menu {
  display: flex;
  margin-right: 12px;
  a {
    text-decoration: none;
  }
}

.is-menu-link {
  display: block;
  padding: 0 12px;
  line-height: 52px;
  font-size: 14px;
  font-weight: 500;
  color: var(--el-text-color-primary);
  transition: color var(--el-transition-duration);
  border-bottom: 2px solid transparent;

  &:hover {
    color: var(--el-color-primary);
  }
}

:deep(.el-dropdown :focus) {
  outline: none;
  box-shadow: none;
}

.login {
  display: flex;
  justify-content: center;
  align-items: center;

  .user-avator {
    margin: 0 12px 0 0px;
  }

  .el-dropdown-link {
    font-size: 14px;
    color: var(--el-text-color-primary);
    transition: color var(--el-transition-duration);
    cursor: pointer;
    display: flex;
    align-items: center;

    .text-margin {
      margin-right: 2px;
    }
  }

  .login-icon {
    &:hover {
      cursor: pointer;
    }
  }
}

.home-menu-link {
  display: block;
  line-height: 52px;
  font-size: 14px;
  font-weight: 500;
  color: var(--el-text-color-primary);
  transition: color var(--el-transition-duration);
  border-bottom: 2px solid transparent;
}

.link-item {
  display: flex;
  align-items: center;
  justify-content: center;
}

.router-link-item {
  @extend .is-menu-link;
  @extend .link-item;
}

.active {
  border-bottom-color: var(--el-color-primary);
}
</style>
